/*****************************************************************************
*    Copyright (C)2003 Ali Corporation. All Rights Reserved.
*
*    File:    nim_tsa5059.c
*
*    Description:    This file contains TSA5059 basic function in LLD.
*    History:
*           Date            Athor        Version          Reason
*	    ============	=============	=========	=================
*	1.  Dec.02.2003		David Wang		 Ver 0.1	Create file.
*****************************************************************************/

#include <sys_config.h>
#include <retcode.h>
#include <types.h>

#include <api/libc/printf.h>
#include <bus/i2c/i2c.h>

#include "nim_tsa5059.h"

#define PLL_STEP  500
struct QPSK_TUNER_CONFIG_EXT * stTSA5059_dev_id[MAX_TUNER_SUPPORT_NUM] = {NULL};
static UINT32 stTSA5059_tuner_cnt = 0;

INT32 nim_ix2360_init(UINT32* tuner_id, struct QPSK_TUNER_CONFIG_EXT * ptrTuner_Config)
{
	struct QPSK_TUNER_CONFIG_EXT * stTSA5059_ptr = NULL;
	if(ptrTuner_Config==NULL||stTSA5059_tuner_cnt>=MAX_TUNER_SUPPORT_NUM)
		return ERR_FAILUE;
	stTSA5059_ptr = (struct QPSK_TUNER_CONFIG_EXT *)MALLOC(sizeof(struct QPSK_TUNER_CONFIG_EXT));
	if(!stTSA5059_ptr)
		return ERR_FAILUE;
	stTSA5059_dev_id[stTSA5059_tuner_cnt] = stTSA5059_ptr;
	MEMCPY(stTSA5059_ptr, ptrTuner_Config, sizeof(struct QPSK_TUNER_CONFIG_EXT));
	*tuner_id = stTSA5059_tuner_cnt;
	stTSA5059_tuner_cnt++;
	return RET_SUCCESS;
}
/*****************************************************************************
* INT32 nim_tsa5059_control(UINT32 freq, UINT8 bs, UINT8 cp)
*
* Tuner write operation
*
* Arguments:
*  Parameter1: UINT32 freq		: Synthesiser programmable divider
*  Parameter2: UINT8 bs			: Baseband path select
*  Parameter3: UINT8 cp			: Charge pump current
*
* Return Value: INT32			: Result
*****************************************************************************/
INT32 nim_tsa5059_control(UINT32 tuner_id, UINT32 freq, UINT32 sym)
{
	UINT8 data[4];
	UINT8 pos;
	UINT16 divider;
	struct QPSK_TUNER_CONFIG_EXT * stTSA5059_ptr = NULL;
	if(tuner_id>=stTSA5059_tuner_cnt||tuner_id>=MAX_TUNER_SUPPORT_NUM)
		return ERR_FAILUE;
	stTSA5059_ptr = stTSA5059_dev_id[tuner_id];
	pos = (freq < 1530 ? 1 : 0);
	divider = freq * 1000/PLL_STEP;

	data[0] = (UINT8)((divider >> 8) & 0x7f);
	data[1] = (UINT8)divider;
	data[2] = 0x82;			/* for XuGuang DSQ-3L2001 */
	data[3] = (UINT8)0x00|(pos<<2);
	if(sym > 20000)
		data[3] = (UINT8)0x80|(pos<<2);
	return i2c_write(stTSA5059_ptr->i2c_type_id, stTSA5059_ptr->cTuner_Base_Addr|0x00, data, 4);/* LSB=0 write mode; LSB=1 read mode */
}

/*****************************************************************************
* INT32 nim_tsa5059_status(UINT8 *lock)
*
* Tuner read operation
*
* Arguments:
*  Parameter1: UINT8 *lock		: Phase lock status
*
* Return Value: INT32			: Result
*****************************************************************************/
INT32 nim_tsa5059_status(UINT32 tuner_id, UINT8 *lock)
{
	INT32 result;
	UINT8 data;
	struct QPSK_TUNER_CONFIG_EXT * stTSA5059_ptr = NULL;
	if(tuner_id>=stTSA5059_tuner_cnt||tuner_id>=MAX_TUNER_SUPPORT_NUM)
	{
		*lock = 0;
		return ERR_FAILUE;
	}
	stTSA5059_ptr = stTSA5059_dev_id[tuner_id];
	result = i2c_read(stTSA5059_ptr->i2c_type_id, stTSA5059_ptr->cTuner_Base_Addr|0x01, &data, 1);/* LSB=0 write mode; LSB=1 read mode */
	*lock = ((data & 0x40) >> 6);

	return result;
}
